package cn.sangedon.review.data.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.sangedon.review.common.constant.TimeConstant;
import cn.sangedon.review.common.domain.bean.DailyTradingTime;
import cn.sangedon.review.common.domain.bean.TradeDateList;
import cn.sangedon.review.common.domain.data.TradingDate;
import cn.sangedon.review.common.service.data.TradingDateService;
import cn.sangedon.review.data.mapper.TradingDateMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.List;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * @author dongliangqiong
 * @description 针对表【tb_trading_date(交易日历)】的数据库操作Service实现
 * @createDate 2022-08-30 22:23:49
 */
@DubboService
public class TradingDateServiceImpl extends ServiceImpl<TradingDateMapper, TradingDate> implements TradingDateService {

    @Override
    public List<String> getLatestTradingDay(Integer num) {
        return this.baseMapper.getLatestTradingDay(num);
    }

    @Override
    public TradeDateList preTradingDay(String time, Integer num) {
        return new TradeDateList(this.baseMapper.preTradingDay(time, num));
    }

    @Override
    public TradeDateList postTradingDay(String time, Integer num) {
        return new TradeDateList(this.baseMapper.postTradingDay(time, num));
    }

    @Override
    public TradeDateList aroundTradingDay(String time, Integer preNum, Integer postNum) {
        List<TradingDate> tradeDays = this.baseMapper.preTradingDay(time, preNum);
        tradeDays.addAll(this.baseMapper.postTradingDay(time, postNum));
        return new TradeDateList(tradeDays);
    }

    @Override
    public TradeDateList betweenTradingDay(String startTime, String endTime) {
        return new TradeDateList(this.baseMapper.betweenTradingDay(startTime, endTime));
    }

    @Override
    public String getTradeDateAgo(String tradeDate, int ageNum) {
        return this.baseMapper.getTradeDateAgo(tradeDate, ageNum);
    }

    @Override
    public Integer distance(String preTradingDate, String postTradingDate) {
        return this.baseMapper.distance(preTradingDate, postTradingDate);
    }

    @Override
    public Boolean isTradingHours(Date date) {
        if (date == null) {
            date = new Date();
        }
        String tradeDate = DateUtil.formatDate(date);
        boolean flag = false;
        long count = this.count(new LambdaQueryWrapper<TradingDate>().eq(TradingDate::getTradingDate, tradeDate));
        if (count == 0) {
            return flag;
        }

        DailyTradingTime[] dailyTradingTimes = TimeConstant.DAILY_TRADING_TIME;
        String time = DateUtil.formatTime(date);
        for (DailyTradingTime dailyTradingTime : dailyTradingTimes) {
            if (DateUtil.parseTime(time).isAfterOrEquals(DateUtil.parseTime(dailyTradingTime.getStartTime())) &&
                DateUtil.parseTime(time).isBeforeOrEquals(DateUtil.parseTime(dailyTradingTime.getEndTime()))) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}




